package savet.gui.tabelas;

import java.awt.Component;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import savet.util.DataUtil;
import sun.swing.DefaultLookup;

/**
 *
 * @author Adisson Marinho
 */
public class DateCellRenderer extends DefaultTableCellRenderer {

    private boolean withDefaultBorder;

    /**
     * Cria renderer sem borda padrao.
     * @see #DateCellRenderer(boolean) 
     */
    public DateCellRenderer() {
        this(false);
    }

    /**
     * Indica se o renderer deve utilizar a borda padrao do JTable.
     * Caso seja false, sera desenhada uma borda semelhante a utilizada no Header da JTable.
     *
     * @param withBorder
     */
    public DateCellRenderer(boolean withBorder) {
        this.withDefaultBorder = withBorder;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component renderer =
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        try {
            String data = DataUtil.converteDateToString((Date) value, DataUtil.FORMATO_PADRAO);
            setValue(data);
        } catch (NullPointerException e) {
            setValue("");
        }
        if (!withDefaultBorder) {
            Border border = null;
            if (hasFocus && border != null) {
                if (isSelected) {
                    border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
                }
            } else {
                border = UIManager.getBorder("TableHeader.cellBorder");
            }
            setBorder(border);
        }
        return renderer;
    }
}
